using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Db4o.Framework;
using Db4o.Framework.Business;
using Db4o.Framework.Configuration;

using SampleBusiness;

namespace SampleApplication
{
    public partial class frmRace : Form
    {
        
        public frmRace()
        {
          
                InitializeComponent();

                // Configure my Business assembly   
                CoreInitializer.Instance.Configure(AssemblySettings.Default["BusinessAssembly"]);

                // Attach the logging machine
                RichTextBoxAppender.SetRichTextBox(ref richLogger);

                ExceptionManager.Instance.ThrowErrorLogException("UnManagedTypeException", "Hello world");

                // Load the database file
                CoreInitializer.Instance.Load(false);

                //populate();

            }

            #region Automatic population

            //private void populate()
            //{
            //    // Close and delete
            //    DataManager.Instance.Close(true);
            //    // RelodLoad the database file
            //    CoreInitializer.Instance.Load(false);
            //    richLogger.Text += "Populate Cars\n";
            //    Car c1 = (Car)BusinessFactory.Instance.CreateInstanceWithId("Car", "Red", 1);
            //    Car c2 = (Car)BusinessFactory.Instance.CreateInstanceWithId("Car", "Red", 2);
            //    Car c3 = (Car)BusinessFactory.Instance.CreateInstanceWithId("Car", "Grey", 5);
            //    Car c4 = (Car)BusinessFactory.Instance.CreateInstanceWithId("Car", "Grey", 3);


            //    richLogger.Text += "Populate Pilots\n";
            //    Pilot p1 = (Pilot)BusinessFactory.Instance.CreateInstanceWithId("Pilot", "Schumacher", 12, c1);
            //    Pilot p2 = (Pilot)BusinessFactory.Instance.CreateInstanceWithId("Pilot", "Masa", 2, c2);
            //    Pilot p3 = (Pilot)BusinessFactory.Instance.CreateInstanceWithId("Pilot", "Alonzo", 10, c3);
            //    Pilot p4 = (Pilot)BusinessFactory.Instance.CreateInstanceWithId("Pilot", "Lewis", 22, c4);

            //    richLogger.Text += "Populate RaceTeams\n";
            //    RaceTeam Ferrari = (RaceTeam)BusinessFactory.Instance.CreateInstanceWithId("RaceTeam", "Ferrari",30);
            //    Ferrari.Pilots.Add(p1);
            //    Ferrari.Pilots.Add(p2);
            //    Ferrari.Store();
            //    RaceTeam McLaren = (RaceTeam)BusinessFactory.Instance.CreateInstanceWithId("RaceTeam", "McLaren",30);
            //    McLaren.Pilots.Add(p3);
            //    McLaren.Pilots.Add(p4);
            //    McLaren.Store();


            //    try
            //    {
            //        #region automatic insert

            //        int count = 0;

            //        if (count > 0)
            //        {
            //            int i = 0; while (i++ < count)
            //            {
            //                RaceTeam team = (RaceTeam)BusinessFactory.Instance.CreateInstanceWithId("RaceTeam", "Team" + i.ToString(), 30 + i);
            //                int p = 0; while (p++ < 2)
            //                {
            //                    Car car = (Car)BusinessFactory.Instance.CreateInstanceWithId("Car", "Color" + p.ToString(), p);
            //                    Pilot pilot = (Pilot)BusinessFactory.Instance.CreateInstanceWithId("Pilot", "Pilot" + p.ToString(), p + i, car);
            //                    team.Pilots.Add(pilot);
            //                }
            //                team.Store();
            //            }
            //            i = 0; while (i++ < count)
            //            {
            //                RaceTeam team = (RaceTeam)BusinessFactory.Instance.CreateInstanceWithId("RaceTeam", "Team" + i.ToString(), 30 + i);
            //                int p = 0; while (p++ < 2)
            //                {
            //                    Car car = (Car)BusinessFactory.Instance.CreateInstanceWithId("Car", "Grey", p);
            //                    Pilot pilot = (Pilot)BusinessFactory.Instance.CreateInstanceWithId("Pilot", "Pilot" + p.ToString(), p + i, car);
            //                    team.Pilots.Add(pilot);
            //                }
            //                team.Store();
            //            }
            //        }

            //        #endregion
            //    }
            //    catch (FormatException ex)
            //    {
            //        System.Diagnostics.Trace.WriteLine(ex.Source + "\n" + ex.Message + "\n" + ex.StackTrace);
            //    }
            //    finally
            //    {
            //    }
            //}

            #endregion

            private void Race_Load(object sender, EventArgs e)
        {
            #region initialisation des composants graphiques

            #endregion
        }
    }
}